package Common;

import Dto.SerializableDTO;
import Util.OutputAndInputFile;

import java.io.*;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;


public class TryUnlock {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        OutputAndInputFile IOUtil = new OutputAndInputFile();
        SerializableDTO dto = (SerializableDTO) IOUtil.tryReadFile();
        try {
            Set<Integer> password = new HashSet<>();
            Set<Integer> TriedPassword = dto.getTriedPasswords();
            System.out.println(dto.getTriedPasswords());
            for (int i = 0; i < 10000; i++) {
                if (TriedPassword.contains(i)) {
                    continue;
                }
                password.add(i);
            }

            Scanner sc = new Scanner(System.in);
            System.err.println("上次最后尝试的密码是：" + dto.getLastTriedPassword() + "\njiang个人规则为：升序尝试");
            while (!password.isEmpty()) {
                System.out.println("请输入需要尝试的密码(99999退出系统)");

                try {
                    int nowPassword = sc.nextInt();
                    if(nowPassword == 99999) {
                        break;
                    }
                    if(nowPassword >= 10000 || nowPassword < 0) {
                        throw new InputMismatchException();
                    }
                    if (TriedPassword.contains(nowPassword)) {
                        System.err.println("此密码已经尝试过！！");
                        System.err.println("—————————————————重新输入———————————————————————");
                        continue;
                    }
                    password.remove(nowPassword);
                    TriedPassword.add(nowPassword);
                    dto.setLastTriedPassword(nowPassword);
                    int count = password.size();
                    System.out.println("———————————————此密码未尝试！！！本次成功完成, 剩余" + count +"个密码未尝试—————————————————");

                } catch (InputMismatchException e) {
                    System.err.println("请输入数字！！！(0 <= num < 10000)");
                } finally {
                    sc.nextLine();
                }
            }
        } finally {
            IOUtil.tryOutFile(dto);
            System.out.println("\n成功输出已尝试过的密码");
        }

    }
}
